package com.renwei.yunlong.activity.news;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.bean.MsgCompanyDetailBean;
import com.renwei.yunlong.event.MsgCompanyStateRefreshEvent;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.CircleTextImage;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class MsgCompanyDetailActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.iv_head)
    CircleTextImage ivHead;
    @Bind(R.id.tv_company_name)
    TextView tvCompanyName;
    @Bind(R.id.tv_detail)
    TextView tvDetail;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.btn_tongyi)
    Button btnTongyi;
    @Bind(R.id.bt_jujue)
    Button btJujue;
    @Bind(R.id.iv_state)
    ImageView ivState;
    private boolean isSend;
    private int relationId;
    private MsgCompanyDetailBean.RowsBean bean;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_company_detail);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        isSend = getIntent().getBooleanExtra("isSend", false);
        relationId = getIntent().getIntExtra("relationId", -1);
        position = getIntent().getIntExtra("position", -1);

        simpleTileView.setTitle(isSend ? "我收到的" : "我申请的");
        initData();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("relationId", relationId + "");
        ServiceRequestManager.getManager().getcompanyMsgDetail(this, JsonMapUtil.mapToJson(map), new HttpTaskListener() {
            @Override
            public void onSuccess(int requestId, String result) {
                MsgCompanyDetailBean bean = new Gson().fromJson(result, MsgCompanyDetailBean.class);
                if (bean.getMessage().getCode() == 200) {
                    setData(bean.getRows());
                } else {
                    showCenterInfoMsg(bean.getMessage().getMessage());
                }
            }

            @Override
            public void onException(int requestId, String code) {

            }
        });
    }

    private void setData(MsgCompanyDetailBean.RowsBean bean) {
        this.bean = bean;
        tvName.setText(bean.getSendName());
        tvPhone.setText(bean.getMobilePhone());
        if ("0".equals(bean.getStatus()) && isSend) {
            btJujue.setVisibility(View.VISIBLE);
            btnTongyi.setVisibility(View.VISIBLE);
            btJujue.setOnClickListener(this);
            btnTongyi.setOnClickListener(this);
        }
        if ("1".equals(companyType)) {
            tvCompanyName.setText(bean.getServerName());
        } else if ("2".equals(companyType)) {
            tvCompanyName.setText(bean.getOwnerName());
        }

        tvDetail.setVisibility(View.GONE);

        tvContent.setText(bean.getRelationDesc());
        tvTime.setText(bean.getCreateTime().replace("T", " "));
        ivHead.setText4CircleImage(String.valueOf(tvCompanyName.getText().toString()));

        if ("0".equals(bean.getStatus())) {
            ivState.setImageResource(R.mipmap.icon_daishenhe);
            ivHead.setTextCircleColor(Color.parseColor("#e64545"));
        } else if ("1".equals(bean.getStatus())) {
            ivState.setImageResource(R.mipmap.icon_yitongguo);
            ivHead.setTextCircleColor(Color.parseColor("#76c56d"));
        } else if ("2".equals(bean.getStatus())) {
            ivState.setImageResource(R.mipmap.icon_yijujue);
            ivHead.setTextCircleColor(Color.parseColor("#999691"));
        } else if ("3".equals(bean.getStatus())) {
            ivState.setImageResource(R.mipmap.icon_yijiebang);
            ivHead.setTextCircleColor(Color.parseColor("#999691"));
        } else {
            ivState.setVisibility(View.GONE);
        }


    }

    public static void OpenActivity(Context context, int id, boolean isSend, int position) {
        Intent intent = new Intent(context, MsgCompanyDetailActivity.class);
        intent.putExtra("relationId", id);
        intent.putExtra("position", position);
        intent.putExtra("isSend", isSend);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_tongyi:
                dealBtn(true);
                break;
            case R.id.bt_jujue:
                dealBtn(false);
                break;
            default:
                break;
        }
    }

    private void dealBtn(final boolean b) {
        if (bean == null) {
            return;
        }
        HttpPresenter hp = new HttpPresenter(this);
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            hp.setUrl(Api.$().OWNER_MESSAGE_COMPANY_DETAIL_DEAL);
            map.put("relationId", bean.getRelationId() + "");
            map.put("status", b ? "1" : "2");
            map.put("ownerCode", ownerBean.getRows().getOwnerCode());
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
            hp.putParam("jsonBean", JsonMapUtil.mapToJson(map));
        } else if ("2".equals(companyType)) {
            hp.setUrl(Api.$().SERVICE_MESSAGE_COMPANY_DETAIL_DEAL);
            map.put("relationId", bean.getRelationId() + "");
            map.put("status", b ? "1" : "2");
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
            hp.putParam("jsonBean", JsonMapUtil.mapToJson(map));
        }
        hp.setshowDialogcontent("正在加载");
        hp.setCallBack(new HttpTaskListener() {
            @Override
            public void onSuccess(int requestId, String result) {
                CommonBean bean = new Gson().fromJson(result, CommonBean.class);
                if (bean.getMessage().getCode() == 200) {
                    EventBus.getDefault().post(new MsgCompanyStateRefreshEvent(b, position));
                    finish();
                } else {
                    showCenterInfoMsg(bean.getMessage().getMessage());
                }
            }

            @Override
            public void onException(int requestId, String code) {

            }
        });
        hp.post();
    }
}
